package com.atguigu.yygh.user.controller;/*
 * @author: XueYouPeng
 * @time: 2023/6/15 14:43
 */

import com.atguigu.common.result.Result;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientController {

    @Autowired
    PatientService patientService;

    //给订单服务调用的
    @GetMapping("getPatientById/{patientId}")
    public Patient getPatientById(@PathVariable Long patientId){
        Patient patient = patientService.getByPatientId(patientId);
        return  patient;
    }

    @GetMapping("getById/{patientId}")
    public Result getById(@PathVariable Long patientId){
        Patient patient = patientService.getByPatientId(patientId);
        return Result.ok().data("patient",patient);
    }

    //查询当前用户就诊人列表
    @GetMapping("/findPatientList")
    public Result findPatientList(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.ok().data("list",list);
    }

    //添加就诊人
    @PostMapping("savePatient")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    @DeleteMapping("removeById/{patientId}")
    public Result removeById(@PathVariable Long patientId){
        patientService.removeById(patientId);
        return Result.ok();
    }

    @PostMapping("updateById")
    public Result updateById(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }
}
